package org.lazy.generate.util;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashSet;
import java.util.Set;

import org.lazy.generate.common.DBType;
import org.lazy.generate.common.JavaType;

/**
 * 
 * @description SQL Types操作工具类
 * @author Anan
 * @time 2013-9-22 下午10:08:18
 */
public class SQLTypesUtil {
	private SQLTypesUtil(){};
	/**
	 * 
	 * @description 获取数据库表列类型对应的java数据类型
	 * @author Anan
	 * @time  2013-9-22 下午10:08:14
	 * @param columnType
	 * @param columnTypeName
	 * @return
	 * @throws Exception
	 */
	public static String getJavaType(int columnType,String columnTypeName,String dbType) throws Exception{
		String javaType = null;
		if(DBType.MYSQL.equals(dbType)){
			javaType = getJavaTypeByMySQL(columnType, columnTypeName);
		}else if(DBType.ORACLE.equals(dbType)){
			javaType = getJavaTypeByOracle(columnType, columnTypeName);
		}else if(DBType.SQLSERVER.equals(dbType)){
			javaType = getJavaTypeBySQLServer(columnType, columnTypeName);
		}
		else{
			throw new Exception("暂时不支持"+dbType+"数据库 !");
		}
		return javaType;
	}
	
	private static String getJavaTypeByOracle(int columnType,String columnTypeName) throws Exception{
		String javaType = null;
		if (Types.INTEGER == columnType||Types.SMALLINT==columnType||Types.TINYINT==columnType) {
			javaType = JavaType.Integer;
		}
		else if(Types.BIGINT == columnType){
			javaType = JavaType.Long;
		}
		else if(Types.DOUBLE == columnType||Types.FLOAT == columnType){
			javaType = JavaType.Double;
		}
		else if(Types.DECIMAL==columnType||Types.NUMERIC==columnType){
			javaType = JavaType.BigDecimal;
		}
		else if(Types.CHAR == columnType){
			javaType = JavaType.Character;
		}
		else if(Types.BIT == columnType){
			javaType = JavaType.Boolean;
		}
		else if (Types.VARCHAR == columnType||Types.CHAR == columnType||Types.CLOB==columnType) {
			javaType = JavaType.String;
		}
		else if(Types.DATE==columnType||Types.TIME==columnType||Types.TIMESTAMP==columnType){
			javaType = JavaType.Date;
		}
		else{
			throw new Exception("getJavaType failed,Cause by:"+columnTypeName+" not match the appropriate type !");
		}
		return javaType;
	}
	private  static String getJavaTypeByMySQL(int columnType,String columnTypeName) throws Exception{
		String javaType = null;
		if (Types.INTEGER == columnType||Types.SMALLINT==columnType||Types.TINYINT==columnType) {
			javaType = JavaType.Integer;
		}
		else if(Types.BIGINT == columnType){
			javaType = JavaType.Long;
		}
		else if(Types.FLOAT == columnType){
			javaType = JavaType.Float;
		}
		else if(Types.DOUBLE == columnType){
			javaType = JavaType.Double;
		}
		else if(Types.DECIMAL==columnType||Types.NUMERIC==columnType){
			javaType = JavaType.BigDecimal;
		}
		else if(Types.CHAR == columnType){
			javaType = JavaType.Character;
		}
		else if(Types.BIT == columnType){
			javaType = JavaType.Boolean;
		}
		else if (Types.VARCHAR == columnType||Types.CHAR == columnType||Types.CLOB==columnType) {
			javaType = JavaType.String;
		}
		else if(Types.DATE==columnType||Types.TIME==columnType||Types.TIMESTAMP==columnType){
			javaType = JavaType.Date;
		}
		else{
			throw new Exception("getJavaType failed,Cause by:"+columnTypeName+" not match the appropriate type !");
		}
		return javaType;
	}
	private static String getJavaTypeBySQLServer(int columnType,String columnTypeName) throws Exception{
		String javaType = null;
		if (Types.INTEGER == columnType||Types.SMALLINT==columnType) {
			javaType = JavaType.Integer;
		}
		else if(Types.BIGINT == columnType){
			javaType = JavaType.Long;
		}
		else if(Types.DOUBLE == columnType||Types.FLOAT == columnType){
			javaType = JavaType.Double;
		}
		else if(Types.DECIMAL==columnType||Types.NUMERIC==columnType){
			javaType = JavaType.BigDecimal;
		}
		else if(Types.CHAR == columnType){
			javaType = JavaType.Character;
		}
		else if(Types.BIT == columnType){
			javaType = JavaType.Boolean;
		}
		else if (Types.VARCHAR == columnType||Types.CHAR == columnType||Types.CLOB==columnType) {
			javaType = JavaType.String;
		}
		else if(Types.DATE==columnType||Types.TIME==columnType||Types.TIMESTAMP==columnType){
			javaType = JavaType.Date;
		}
		else{
			throw new Exception("getJavaType failed,Cause by:"+columnTypeName+" not match the appropriate type !");
		}
		return javaType;
	}
	/**
	 * 
	 * @description 根据数据库表反向生成dto类需要import的类
	 * @author Anan
	 * @time  2013-9-22 下午10:09:57
	 * @param metaData
	 * @return
	 * @throws Exception 
	 */
	public static Set<String> needImportClass(ResultSetMetaData metaData,String dbType) throws Exception{
		Set<String> sets = new HashSet<String>();
		int colum;
		try {
			colum = metaData.getColumnCount();
			for (int i = 1; i <= colum; i++) {
				int columnType = metaData.getColumnType(i);
				String columnTypeName = metaData.getColumnName(i);
				String javaType = getJavaType(columnType, columnTypeName, dbType);
				if(JavaType.Date.equals(javaType)){
					sets.add("java.util.Date");
				}else if(JavaType.BigDecimal.equals(javaType)){
					sets.add("java.math.BigDecimal");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sets;
	}
	
}
